const FileUtils = require('../webpackPlugin/utils/FileUtils.js')
const UtilsIndex = require('../webpackPlugin/utils/index.js')
// 遍历
const traverse = require('babel-traverse').default;
const t = require('babel-types');

// // 覆盖pageJson文件文件里面的pages
module.exports = function () {
  const pages = []
  const findSync = FileUtils.findSync(FileUtils.join(__dirname, 'module'));
  for (let findSyncElement of findSync) {
    const buffer = FileUtils.fileToString(findSyncElement);
    const scriptAst = UtilsIndex.getScriptAst(buffer);
    traverse(scriptAst, {
      ArrowFunctionExpression(ipath) {
        const parent = ipath.parent
        if (parent && parent.type === 'ObjectProperty' && parent.key.value === 'path') {
          const node = ipath.node
          const newPath = node.body.arguments[0].value.replace('@/', '').replace('.vue', '')
          parent.value = t.stringLiteral(newPath)
        }
      }
    })
    const astSyncCode = UtilsIndex.getAstSyncCode(scriptAst);
    const functionStr = `
    ${astSyncCode}
    return pages
    `
    const getPages = new Function('', functionStr)
    pages.push(...getPages())
  }
  return pages
}
